
public class e037 {
    public static void main(String[] args) {
        long sTime = System.currentTimeMillis();
        int sum = 0;
        for (int i = 11; i < 1000000; i += 2) {
            if(isPrime(i)) {
                if (truncRL(i) && truncLR(i)) {
                    sum += i;
                }
            }
        }
        System.out.println(sum);
        long fTime = System.currentTimeMillis();
        System.out.println("Time: " + (fTime - sTime) + "ms");
    }


    static boolean truncRL(int i) {
        String l = String.valueOf(i);
        if (l.contains("11") || l.contains("33") || l.contains("77") || l.contains("99") || l.charAt(l.length() - 1) == '1' || l.charAt(0) == '1') {
            return false;
        }
        StringBuilder b = new StringBuilder(l);
        for (int j = l.length() - 1; j > 0; j--) {
            b.deleteCharAt(j);
            int n = Integer.parseInt(b.toString());
            if (!isPrime(n)) {
                return false;
            }
        }
        return true;
    }

    static boolean truncLR(int i) {
        String l = String.valueOf(i);
        if (l.contains("11") || l.contains("33") || l.contains("77") || l.contains("99") || l.charAt(l.length() - 1) == '1' || l.charAt(0) == '1') {
            return false;
        }
        StringBuilder b = new StringBuilder(l);
        for (int j = 0; j < l.length() - 1; j++) {
            b.deleteCharAt(0);
            int n = Integer.parseInt(b.toString());
            if (!isPrime(n)) {
                return false;
            }
        }
        return true;
    }

    static boolean isPrime(int i) {
        for (int j = 2; j <= Math.sqrt(i); j++) {
            if (i % j == 0) {
                return false;
            }
        }
        return true;
    }
}